<template>
  <div class="app-container">
    <div class="card-box">
      <el-button type="primary" @click="handleToAdd">添加充值订单</el-button>
    </div>
    <div class="card-box">
      <el-table
        v-loading="rechargeStore.loading.list || rechargeStore.loading.update"
        :data="rechargeStore.list"
        row-id="id"
      >
        <el-table-column label="ID" prop="id" />
        <el-table-column label="用户">
          <template #default="{ row }">
            {{ row.user.username }}
          </template>
        </el-table-column>
        <el-table-column label="充值金额" prop="amount" />
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-tag v-if="row.status == 'pending'" type="info">待支付</el-tag>
            <el-tag v-if="row.status == 'completed'" type="success">已完成</el-tag>
            <el-tag v-if="row.status == 'canceled'" type="danger">已取消</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleToEdit(row)">编辑</el-button>
            <el-popconfirm title="是否继续删除？" @confirm="handleDelete(row)">
              <template #reference>
                <el-button type="danger" link>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.size"
        style="margin-top: 10px"
        background
        :total="rechargeStore.totalCount"
        @change="rechargeStore.fetchRechargeList(params)"
      />
    </div>
    <Form
      v-model="visible"
      :type="formType"
      :form="currentForm"
      @confirm="rechargeStore.fetchRechargeList(params)"
    />
  </div>
</template>

<script setup lang="ts">
import {
  type RechargeListQuery,
  type RechargeForm,
  type RechargeItemType,
} from "@/api/user/recharge";
import { useRechargeStoreHook } from "@/store/modules/user/recharge";
import Form from "./components/form.vue";
import type { FormType } from "./components/form.vue";

const rechargeStore = useRechargeStoreHook();

const visible = ref(false);

const formType = ref<FormType>("add");

const currentForm = ref<RechargeForm>({
  id: 0,
  user: 0,
  payment_id: 0,
  amount: "",
});

const params = reactive<RechargeListQuery>({
  page: 1,
  size: 10,
});

function handleToAdd() {
  formType.value = "add";
  visible.value = true;
}

async function handleDelete(row: RechargeItemType) {
  await rechargeStore.fetchDeleteRecharge(row.id);
  await rechargeStore.fetchRechargeList(params);
}

function handleToEdit(row: RechargeItemType) {
  formType.value = "edit";
  currentForm.value = {
    id: row.id,
    user: row.user.id,
    amount: row.amount,
    payment_id: row.payment_id,
  };
  visible.value = true;
}

onMounted(() => {
  rechargeStore.fetchRechargeList(params);
});
</script>

<style scoped lang="scss"></style>
